<?php
class model_news extends framework_base_model {

	private static $table = 'weixin_news';
	private static $category_table = 'weixin_category';
	
	
	/**
	 * 通过栏目名称获取栏目ID
	 * @param string $catName
	 * @return int $catId category_id
	 */
	public static function get_catid_by_catName($catName) {
		$catId = 0;
		$sql = "select category_id from ".self::$category_table." where category_name=:category_name and delete_flag=0";
		try {
			$st = self::DB()-> dbh ->prepare($sql);
			$binds = array(':category_name' => $catName);
			$st -> execute($binds);
			$catId = $st->fetchColumn();
		} catch (Exception $e) {
			framework_static_function::write_log($e->getMessage(),'weixin_sql_error');
		}
		return $catId;
	}

    /**
     * 通过栏目ID获取栏目名次
     * @param string $catName
     * @return int $catId category_id
     */
    public static function get_catName_by_catid($catId) {
        $catName = '';
        $sql = "select category_name from ".self::$category_table." where category_id=:category_id and delete_flag=0";
        try {
            $st = self::DB()-> dbh ->prepare($sql);
            $binds = array(':category_id' => $catId);
            $st -> execute($binds);
            $catName = $st->fetchColumn();
        } catch (Exception $e) {
            framework_static_function::write_log($e->getMessage(),'weixin_sql_error');
        }
        return $catName;
    }
    /**
     * 获取子类列表
     * @param int $category_id
     * @return array $articles
     */
    public static function getSonList($catId) {

        $sql = "select category_id,category_name from ".self::$category_table." where parent_id = :parent_id and delete_flag = 0  order by raw_add_time desc ";

        try {
            $st = self::DB()->dbh->prepare($sql);
            $binds = array(':parent_id' => $catId);
            $st->execute($binds);
            $articles = $st->fetchAll();
        } catch (Exception $e) {
            framework_static_function::write_log($e->getMessage(),'weixin_sql_error');
        }
        return $articles;
    }



	/**
	 * 通过栏目ID获取栏目图文新闻
	 * @param int $category_id
	 * @return array $articles
	 */
	public static function get_news_by_catId($category_id,$pageSize,$showPages=true,$visible='*_*') {
		$category_id = intval($category_id);
		$articles = array();
		if($showPages) {
			$sql = "select id,title,digest,picurl,content_source_url from ".self::$table." where category_id = :category_id and delete_flag = 0 and (visible='*_*' or visible like '%".$visible."%') order by article_index desc,raw_add_time desc limit 0,".$pageSize;
		} else {
			$sql = "select id,title,digest,picurl,content_source_url from ".self::$table." where category_id = :category_id and delete_flag = 0 and (visible='*_*' or visible like '%".$visible."%') order by article_index desc,raw_add_time desc";
		}
		//framework_static_function::write_log($sql,"sql");
		/*if($showPages) {
			$sql = "select id,title,digest,picurl,content_source_url from ".self::$table." where category_id = :category_id and delete_flag = 0 order by article_index desc,raw_add_time desc limit 0,".$pageSize;
		} else {
			$sql = "select id,title,digest,picurl,content_source_url from ".self::$table." where category_id = :category_id and delete_flag = 0 order by article_index desc,raw_add_time desc";
		}*/
		
		try {
			$st = self::DB()->dbh->prepare($sql);
			$binds = array(':category_id' => $category_id);	
			$st->execute($binds);
			$articles = $st->fetchAll();		
		} catch (Exception $e) {
			framework_static_function::write_log($e->getMessage(),'weixin_sql_error');
		}
		return $articles;
	}
		
	/**
	 * 根据ID获取新闻内容
	 * @param int $id
	 * @return array $info;
	 */
    public static function getNewsInfoById($id) {
    	$info = array();
    	if(intval($id) <= 0) {
    		return $info;
    	}
    	$sql = "select * from ".self::$table." where id=:id and delete_flag = 0";
    	try {
    		$st = self::DB() -> dbh -> prepare($sql);
    		$st -> execute(array(":id" => intval($id)));
    		$info = $st -> fetch();
    	} catch (Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
    	return $info;
    }
}
